package com.lizardmind.haofudoctor.ui.other

import android.app.Activity
import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.view.View
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.getDataBinding
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityEvaluationBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.sdk25.coroutines.onClick
import java.io.Serializable

/**
 * 评价界面
 * Created by Extends on 2017/11/20 17:20
 */
class EvaluationActivity:BaseActivity() {
    private val binding by lazy { getDataBinding<ActivityEvaluationBinding>(R.layout.activity_evaluation) }
    override fun onCreatRootView(): View=binding.root
    private lateinit var fatherType:String
    private lateinit var fatherId:String

    override fun onInitView(savedInstanceState: Bundle?) {
        //初始化标题栏
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "评价"

        binding.apply {
            vm = ViewModelProviders.of(this@EvaluationActivity).get(EvaluationVM::class.java)
            val bean : Serializable? = intent.getSerializableExtra("bean")
            fatherType = intent.getStringExtra("fatherType")?:"1"
            fatherId = intent.getStringExtra("fatherId")
            if(bean!=null){
                vm?.bean?.set(bean as DoctorBean)
            }

            lifecycle.addObserver(vm!!)

            aeEvaluation.onClick {
                observe {
                    postApi<Request<String>>(hashMapOf(
                            "action" to "create_evaluation",
                            "token" to DoctorInfo.token,
                            "uid" to "1",
                            "type" to fatherType,
                            "type_id" to fatherId,
                            "experience" to vm!!.experience.get(),
                            "simple_experience" to vm!!.select.get().toString()
                    ), Consumer {
                        it.dispose {
                            setResult(Activity.RESULT_OK,intentFor<String>("evauationId" to it.toInt()))
                            finish()
                        }
                    })
                }
            }

        }
    }
}